/*
 * Player.cpp
 *
 *  Created on: 24 jan 2010
 *      Author: Fidd
 */

#include "Player.h"
#include <iostream>
#include <math.h>
#include <time.h>

// Only test
Player::Player(int type)
: Sprite() {


	if (type == SHIP_RED) {
		loadImage("Sprites/spaceship.png");
	} else if (type == SHIP_BLUE) {
		loadImage("Sprites/spaceship_blue.png");
	}

	for(int i=0;i<5;i++){
		keysetup[i]=0;
	}

	explosion = new Sprite("img/space/explosion17.png");
	explosion->loadAnimation(5,5,20);
	ypos=rand()%500+1;
	xpos=rand()%500+1;
	firetime=20;
	allowfire=0;
	firepos=15;
	sendbullet=0;

	radius = 20;

	resetKeys();

	cout << "Player created" << endl;
}

Player::Player(int keys[], int type)
: Sprite() {

	if (type == SHIP_RED) {
		loadImage("Sprites/spaceship.png");
	} else if (type == SHIP_BLUE) {
		loadImage("Sprites/spaceship_blue.png");
	}

	for(int i=0;i<5;i++){
		keysetup[i]=keys[i];
	}

	explosion = new Sprite("Sprites/explosion17.png");
	explosion->loadAnimation(5,5,20);
	ypos=rand()%500+1;
	xpos=rand()%500+1;
	firetime=20;
	allowfire=0;
	firepos=15;
	sendbullet=0;

	radius = 20;

	resetKeys();

	cout << "Player created" << endl;
}

Player::~Player() {
	// TODO Auto-generated destructor stub
}

	//void setCollisionmap( ){
		//collisionmap=c;
	//}

void Player::updatePos(int elapsed){
	if(allowfire>0){
		allowfire=allowfire-1;
	}
	dy=dy*.988f;
	dx=dx*.988f;

	// Up
	if(keysDown[0]){
		updateSpeed(0, 0.2);
		blast.push_back(new Particle());
		blast.back()->moveTo((float)(20*cos(PI*(angle+180)/180)+xpos+rand()%12 -6),(float)(20*sin(PI*(angle)/180)+ypos+rand()%12 -6));
		blast.back()->setSpeed(dx,dy);
		blast.back()->updateSpeed(angle-180,2);
	}
	// Left
	if(keysDown[1]){
		updateSpeed(4, 0);
	}
	// Down
	if(keysDown[2]){
		updateSpeed(0, -0.05);
	}
	// Right
	if(keysDown[3]){
		updateSpeed(-4, 0);
	}
	// Fire
	if(keysDown[4]){
		if(allowfire==0){
			firepos = 0;
			bullets.push_back(new Bullet(0,angle,dx,dy));
			bullets.back()->moveTo((float)(20*cos(PI*(angle)/180)+xpos+firepos*sin(PI*(angle)/180)),(float)(20*sin(PI*(angle+180)/180)+ypos+firepos*cos(PI*(angle)/180)));
			allowfire=firetime;
			sendbullet++;
			if (firepos==15){
				firepos=-15;
			}else{
				firepos=15;
			}

		}
	}

	Sprite::updatePos();
	explosion->getAnimation()->update(elapsed);
	explosion->moveTo(xpos, ypos);
	for (int i=0; i<blast.size(); i++){
		if(blast[i]->update()==false){
			delete blast[i];
			blast.erase(blast.begin()+i);
		}
	}
	for (int i=0; i<bullets.size(); i++){
		bullets[i]->updatePos();
	}

}

void Player::addBullet(int x, int y, float angle, int dx, int dy, int type) {
	bullets.push_back(new Bullet(0,angle,dx,dy,false));
	bullets.back()->moveTo(x,y);
}

void Player::setKeysDown(Uint8 *keysPressed){
	for (int i=0; i<5; i++) {
		keysDown[i] = keysPressed[keysetup[i]];
	}
}

void Player::setKeysDown(int keysPressed[]){
	for (int i=0; i<5; i++) {
		keysDown[i] = keysPressed[i];
	}
}

void Player::resetKeys() {
	for (int i=0; i<5; i++) {
		keysDown[i] = 0;
	}
}

int Player::getKeyDown(int key) {
	return keysDown[key];
}

void Player::setKey(int binding, int key) {
	keysetup[binding] = key;
}

void Player::draw() {
	Sprite::draw();
	for (int i=0; i<bullets.size(); i++) {
			bullets[i]->draw();
		}
	for (int i=0; i<blast.size(); i++) {
		blast[i]->draw();
	}
}

void Player::setName(string n) {
	name = n;
}
string Player::getName() {
	return name;
}

